package com.example.springbootdemo;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author brianxia
 * @version 1.0
 * @date 2022-11-07 14:20
 */
@RestControllerAdvice //advice通知 切点+切面
public class MyControllerAdvice {

    //对异常进行处理
    @ExceptionHandler(Exception.class)
    public Result result1(Exception e){
        Result result = new Result();
        result.setMsg("其他错误");
        result.setCode(3000);
        return result;
    }
    //处理自定义异常
    @ExceptionHandler(MyException.class)
    public Result result2(MyException e){
        e.printStackTrace();
        Result result = new Result();
        result.setMsg(e.getMsg());
        result.setCode(e.getCode());
        return result;
    }
}
